package com.clei.utils.helper;

import com.clei.utils.FileUtil;
import com.clei.utils.PrintUtil;
import com.clei.utils.S3Util;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Scanner;

/**
 * minio数据迁移到 amazon s3
 * rclone 报错 没找到解决方案
 * 只好自己写代码同步了 http下载 sdk上传
 *
 * @author Y
 * @date 2024-09-10
 **/
public class Minio2AmazonS3 {

    public static void main(String[] args) {
        // 1. bucket下全部数据同步
        // 2. bucket下部分数据同步
        int mode = 2;
        if (null != args && args.length > 0) {
            mode = Integer.parseInt(args[0]);
        }
        // 入参
        Scanner input = new Scanner(System.in);
        PrintUtil.print("Minio URL : ");
        String minioUrl = input.nextLine();
        PrintUtil.print("Data File Path : ");
        String filePath = input.nextLine();
        PrintUtil.print("Object Prefix : ");
        String prefix = input.nextLine();
        input.close();
        PrintUtil.log("minioUrl : {}, filePath : {}, prefix: {}", minioUrl, filePath, prefix);

        switch (mode) {
            case 1:
                sync(minioUrl, prefix, new File(filePath), true);
                break;
            case 2:
                sync(minioUrl, prefix, new File(filePath), false);
                break;
            default:
                PrintUtil.log("未知模式");
        }
    }

    /**
     * sync
     *
     * @param minioUrl minioUrl
     * @param prefix   prefix
     * @param file     file
     * @param root     是否是bucket root true/false
     */
    private static void sync(String minioUrl, String prefix, File file, boolean root) {
        if (file.isFile()) {
            PrintUtil.log("未知文件 : {}", file.getAbsolutePath());
            return;
        }
        File[] files = file.listFiles();
        if (null == files) {
            PrintUtil.log("空文件夹 : {}", file.getAbsolutePath());
            return;
        }
        boolean dataObj = Arrays.stream(files).anyMatch(f -> "xl.meta".equals(f.getName()));
        if (dataObj) {
            String objKey = prefix + file.getName();
            upload(minioUrl, objKey);
        } else {
            prefix = root ? "" : prefix + file.getName() + "/";
            for (File f : files) {
                sync(minioUrl, prefix, f, false);
            }
        }
    }

    /**
     * upload
     *
     * @param minioUrl minioUrl
     * @param key      fileKey
     */
    private static void upload(String minioUrl, String key) {
        String fileUrl = minioUrl + key;
        long length = FileUtil.getHttpFileLength(fileUrl);
        if (length <= 0) {
            PrintUtil.log("获取文件信息失败 fileUrl : {}", fileUrl);
            return;
        }
        InputStream inputStream = null;
        try {
            URL url = new URL(fileUrl);
            inputStream = url.openStream();
            // 上传
            S3Util.put(key, inputStream, length);
        } catch (Exception e) {
            PrintUtil.log("获取文件数据失败 fileUrl : {}", fileUrl, e);
        }
    }
}
